//
//  FGNewVoucherViewController.m
//  Mi6App
//
//  Created by Mr.Lemon on 7/25/13.
//  Copyright (c) 2013 4G Secure. All rights reserved.
//

#import "FGNewVoucherViewController.h"

@interface FGNewVoucherViewController ()

@end

@implementation FGNewVoucherViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_common.png"]]];

    [self loadDemoData];
    [self loadLastestVoucher];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(goToYourVoucher) name:@"go_to_your_voucher" object:nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark load Data
-(void)loadDemoData{
 
     FGVoucherInfo *voucher1=[[FGVoucherInfo alloc] initWithId:@"1" title:@"Sains Burys" desc:@"Sains Burys Name" url_image:@"NewVoucher_Sainsburys.png" andExpiredDate:@""];
         FGVoucherInfo *voucher2=[[FGVoucherInfo alloc] initWithId:@"1" title:@"Majestic" desc:@"Majestic Name" url_image:@"NewVoucher_Majestic.png" andExpiredDate:@""];
         FGVoucherInfo *voucher3=[[FGVoucherInfo alloc] initWithId:@"1" title:@"Pizza" desc:@"Pizza Name" url_image:@"NewVoucher_Pizza.png" andExpiredDate:@""];
         FGVoucherInfo *voucher4=[[FGVoucherInfo alloc] initWithId:@"1" title:@"TrackDay " desc:@"TrackDay Name" url_image:@"NewVoucher_TrackDay.png" andExpiredDate:@""];
         FGVoucherInfo *voucher5=[[FGVoucherInfo alloc] initWithId:@"1" title:@"Yoga" desc:@" Yoga Name" url_image:@"NewVoucher_Yoga.png" andExpiredDate:@""];
    self.arrLastVoucher=[[NSMutableArray alloc] initWithObjects:voucher1,voucher2,voucher3,voucher4,voucher5, nil];
}
#pragma mark - Load UI
- (void)loadLastestVoucher {
    
    for (int i = 0; i < self.arrLastVoucher.count; i++) {
        
        NSArray* nibViews =  [[NSBundle mainBundle] loadNibNamed:@"FGLastestVoucherView" owner:self options:nil];
        FGLastestVoucherView *voucherView = [nibViews objectAtIndex:0];
        FGVoucherInfo *voucherInfo = [self.arrLastVoucher objectAtIndex:i];
        voucherView.frame = CGRectMake(scrollViewMain.frame.size.width*i, 0, scrollViewMain.frame.size.width, scrollViewMain.frame.size.height);
        voucherView.delegate=self;
        [voucherView setViewWithVoucherObject:voucherInfo andIndex:i];
        [scrollViewMain addSubview:voucherView];
    }
    
    pageControl.numberOfPages = self.arrLastVoucher.count;
    
    [scrollViewMain setContentSize:CGSizeMake(scrollViewMain.frame.size.width*self.arrLastVoucher.count, scrollViewMain.frame.size.height)];
}


#pragma mark - Scrollview delegate
- (void)scrollViewDidEndDecelerating:(UIScrollView*)scrollView {
    int index = scrollView.contentOffset.x / scrollView.frame.size.width;
    pageControl.currentPage = index;
}



- (void)viewDidUnload {
    scrollViewMain = nil;
    pageControl = nil;
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [super viewDidUnload];
}

#pragma mark Lastest Voucher Delegate
-(void)onClickVoucherWithType:(NSInteger)type andIndex:(NSInteger)indexVoucher{
    DEBUGLog(@"On Click : %d with index %d",type,indexVoucher);
    
    FGNoticeActionViewController *viewController =[[FGNoticeActionViewController alloc] initWithNibName:@"FGNoticeActionViewController" bundle:nil];
    viewController.action=type;

    [self presentModalViewController:viewController animated:YES];
    

    
    //Test add FGShareView and FGRedemView
    
//    NSArray* nibViews = [[NSBundle mainBundle] loadNibNamed:@"FGShareView"
//                                                      owner:self
//                                                    options:nil];
//    FGRedemView  *view=(FGRedemView*)[nibViews objectAtIndex:0];
//    view.backgroundColor=[UIColor clearColor];
//    [self.view.window addSubview:view];
//    FGShareView  *shareView=(FGShareView*)[nibViews objectAtIndex:0];
//    shareView.backgroundColor=[UIColor clearColor];
//    [self.view.window addSubview:shareView];

}

#pragma mark Go To Your Voucher Screen when get notification
-(void)goToYourVoucher{
    
    FGYourVoucherViewController *viewController=[[FGYourVoucherViewController alloc] initWithNibName:@"FGYourVoucherViewController" bundle:nil];
    
    [self.navigationController pushViewController:viewController animated:YES];
}
@end
